package org.uni_hi.muus.server;

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnectionNotifier;


public abstract class Server extends Thread{
	
	protected StreamConnectionNotifier _scn = null;
	
	public Server(String url) throws IOException{
		this(url,Connector.READ_WRITE,true);
	}
	
	public Server(String url,int mode,boolean timeouts) throws IOException{
		_scn =(StreamConnectionNotifier)Connector.open(url,mode,timeouts);
		this.start();
	}
	
	public void pauseApp(){
		
	}
	
	public void destroyApp(boolean unconditional){
		if(_scn!=null){
			try {
				_scn.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	protected abstract void openAndAccept() throws IOException;
	
	public void run() {
			while (true) {
				try {
					openAndAccept();
				} catch (Exception e) {
					//Handle Exceptions any other way you like.
					//No-op
				}
			}
	}
	
}
